<?php
namespace app\behavior;

use \think\Controller;
use think\Hook;
use think\Session;
use \think\Cookie;
use think\Db;

/**
 * 基本行为
 */
class Base extends Controller
{
    public function run(&$data)
    {
        //接单通知
        //queue('app\common\job\JobPay@ReceiveWish', $data, $delay = 0, $queue = 'order'); 
        Hook::add('app_init','app\\behavior\\Nest');
        echo "string";
    }
    /**
     * [bindcard 第一次登陆绑定房车]
     * @param string $value [description]
     */
    public function bindcard(&$data)
    {
        $first   = Cookie::get('firstlogin');
        $user_id = session('user.id');
        //车有没有用户id
        $caravan_user_id = Db::table('caravan')->where('user_id',$user_id)->value('id');
        if (!empty($first)&&empty($caravan_user_id)) {
            return $this->success('请绑定您的房车',url("/userinfo/bindcard",'','','user'));
        }
    }
    /**
     * [checkbindcard 判定用户是否绑定房车（卡）]
     * @param string $value [description]
     */
    public function checkbindcard()
    {
        $user_id = session('user.id');
        //车有没有用户id
        $caravan_user_id = Db::table('caravan')->where('user_id',$user_id)->value('id');
        if (empty($caravan_user_id)) {
            return $this->success('请绑定您的房车',url("/userinfo/bindcard",'','','user'));
        }else{
            //绑定成功用户status = 1，默认为 0 。
            $a = Db::table('users')->where('id',$user_id)->value('status');
            if (!$a) {
            return $this->success('请分享您的房车的旺季时间',url("/userinfo/sethotdate",'','','user'));
            }
        }
    }
    /**
     * [FunctionName 绑定成功修改用户相应字段]
     * @param string $value [description]
     */
    public function UpdateUserInfo($car_id)
    {
       $carinfo   = Db::table('caravan')->where('id',$car_id)->find();
       $campinfo = Db::table('camps')->where('id',$carinfo['services'])->find();
       $result    = Db::table('users')->where('id',session('user.id'))->update([
        'camp_id'=>$carinfo['services'],
        'camp_name'=>$campinfo['name'],
        'park_num'=>$carinfo['park_num'],
        'region'    =>$campinfo['area']
        ]);
    }
    /**
     * [updatestatus 绑定成功修改卡、车状态]
     * @param  string $value [description]
     * @return [type]        [description]
     */
    public function updatestatus($number)
    {
        $cardinfo = Db::table('cards')->where('card_num',$number)->find();
        Db::table('caravan')->where('id',$cardinfo['caravan_id'])->update(['status'=>1]);
        Db::table('cards')->where('card_num',$number)->update(['status'=>1]);
    }
}